﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Inspection
{
    public class DInspectionStructure
    {
        public static DataSet SelectAll(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_InspVisit_XMissionStructures_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
              

                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_InspVisit_XMissionStructures_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static void Delete(Int32 structureId,string connectionKey)
        {

            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_InspVisit_XmissionStructures_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructureId", ParameterDirection.Input, false, structureId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void DeleteWithPoles(Int32 structureId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_InspVisit_XmissionStructures_Delete_WithPoles", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructureId", ParameterDirection.Input, false, structureId));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
            
        }
    }
}
